/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.ziyun.pop.modules.micro.service.imp;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyun.pop.common.utils.MD5;
import com.ziyun.pop.modules.micro.dao.MicroDao;
import com.ziyun.pop.modules.micro.entity.BaseResultVO;
import com.ziyun.pop.modules.micro.entity.ConfigAnonymousRequestEntity;
import com.ziyun.pop.modules.micro.entity.UserInfo;
import com.ziyun.pop.modules.micro.service.RegisterService;
import com.ziyun.pop.modules.storeMember.dao.ErpUserBaseInfoDao;
import com.ziyun.pop.modules.storeMember.entity.UserBaseInfoEntity;
//import com.ziyun.pop.modules.systemConfig.entity.SystemConfigEntity;
//import com.ziyun.pop.modules.systemConfig.service.SystemConfigService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Map;


@Service("registerService")
@Transactional(readOnly = true)
public class RegisterServiceImpl extends ServiceImpl<ErpUserBaseInfoDao, UserBaseInfoEntity> implements RegisterService {

    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private MicroDao microDao;
//	@Autowired
//    private SystemConfigService systemConfigService;

	@Transactional(readOnly = false)
    public BaseResultVO regist(Map<String, Object> map) {
        logger.info("------------regist member start-------------------");
        logger.info("------------param：" + map.toString());
        int res = 0;
        UserInfo userInfo = null;
        BaseResultVO baseResultVO = new BaseResultVO();
        String message = null;
        String name = (String) map.get("name");
        String password = (String) map.get("password");
        String id = (String) map.get("id");
        String mobile = (String) map.get("mobile");
        String ipAddress = (String) map.get("ipAddress");
        //系统基础配置表system_config
//        SystemConfigEntity configSystem = systemConfigService.selectById(22);
//        logger.info("------------configSystem：" + configSystem.toString());
        //内部匿名钥匙表config_anonymous_request
        ConfigAnonymousRequestEntity configAnonymousRequest = microDao.get(id);
        logger.info("------------configAnonymousRequest：" + configAnonymousRequest.toString());
        // configSystem.setParamValue("192.168.1.36:1050");
        // HttpPost httpPost = new HttpPost("http://" + configSystem.getParamValue() +
        // "/api/register");
        HttpPost httpPost = new HttpPost("http://" + "116.62.117.145:1050" + "/ucenter/user/registerByErp");
        httpPost.setHeader("apptype", "erp");
        httpPost.setHeader("anonymouskey", configAnonymousRequest.getAnonymousKey());
        JSONObject json = new JSONObject();
        json.put("account", name);
        json.put("password", MD5.getMD5Code(password));
        json.put("ip", ipAddress);
        json.put("phone", mobile);
        // 'common'一般用户0,'shop'图文店用户1,'company'企业用户2,'designer'设计师3,'supplier'供应商4,'pop'供应商pop平台5,'other'第三方加盟商5,'inside'内部
        // 用户类型
        json.put("userType", 0);
        if (null != map.get("supplierId")) {
            json.put("supplierId", map.get("supplierId"));
        }
        logger.info("------------http json param" + json.toString());
        StringEntity entity = new StringEntity(json.toJSONString(), "utf-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        HttpClient httpClient = HttpClients.createDefault();
        HttpResponse httpresponse = null;
        try {
            httpresponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpresponse.getEntity();
            String response = EntityUtils.toString(httpEntity, "utf-8");
            JSONObject resjson = JSONObject.parseObject(response);
            logger.info("------------http json result" + resjson.toString());
            res = resjson.getInteger("code");
            userInfo = resjson.getObject("data", UserInfo.class);
            message = resjson.getString("message");
            System.out.println(response);
        } catch (ClientProtocolException e) {
            System.out.println("http请求失败，uri{},exception{}");
            logger.error("http请求失败", e);
        } catch (IOException e) {
            System.out.println("http请求失败，uri{},exception{}");
            logger.error("http请求失败", e);
        }
        baseResultVO.setCode(res);
        baseResultVO.setData(userInfo);
        baseResultVO.setMessage(message);
        logger.info("------------baseResultVO" + baseResultVO.toString());
        logger.info("------------regist member end-------------------");
        return baseResultVO;
    }

}